package cn.dai;

import java.io.*;

/**
 * @author ArkD42
 * @file 1 - Java基础语法
 * @create 2020 - 04 - 21 - 16:20
 */
public class IOTest {
    public static void main(String[] args) throws Exception {
        //printStream("sample.txt");
        //printData();

        readData();
    }

    static void fileCopy(String srcPath,String destPath){ // 文件复制的方法封装
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try{
            inputStream = new FileInputStream(srcFile);
            outputStream = new FileOutputStream(destFile);
            byte[] bytesBuffer = new byte[1024]; //2的十次方
            int length; // 当前读取个数
            while ((length = inputStream.read(bytesBuffer)) != -1){
                outputStream.write(bytesBuffer,0,length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static void bufferFileCopy(String srcPath,String destPath){ // 文件复制的方法封装
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        Reader reader = null;
        Writer writer = null;

        BufferedReader bufferReader = null;
        BufferedWriter bufferWriter = null;

        try{
            reader = new FileReader(srcFile);
            writer = new FileWriter(destFile);

            // 多添加一次缓冲流对象装载
            bufferReader = new BufferedReader(reader);
            bufferWriter = new BufferedWriter(writer);

            // 调用缓冲输入输出读写文件
            /*byte[] bytesBuffer = new byte[1024]; //2的十次方
            int length; // 当前读取个数
            while ((length = bufferInput.read(bytesBuffer)) != -1){
                bufferOutput.write(bytesBuffer,0,length);
            }*/

            // 采用String读取
            String data;
            while( (data = bufferReader.readLine()) != null){
                System.out.println(data);
                bufferWriter.write(data + "\n");// data 不包含换行字符
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferReader.close();
                bufferWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static void readFile(String srcPath) throws Exception {
        File file = new File(srcPath);
        InputStream inputStream = new FileInputStream(file);
        InputStreamReader inputReader = new InputStreamReader(inputStream,"UTF-8");
        // new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        // new InputStreamReader(inputStream, "UTF-8"); 一般不写字符集参数，默认使用系统字符集[文件创建时，设置的字符集]
        char[] charsBuffer = new char[12];
        int len;
        while ((len = inputReader.read(charsBuffer)) != -1 ){
            String str = new String(charsBuffer,0,len);
            System.out.println(str);
        }
        inputReader.close();
    }

    static void transFileCopy(String srcPath,String destPath) throws Exception {
        // 创建文件、创建流对象
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(destFile);

        InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"GBK");

        // 读写过程
        char[] charsBuffer = new char[10];
        int len;
        while ((len = inputStreamReader.read(charsBuffer)) != -1){
            outputStreamWriter.write(charsBuffer,0,len);
        }

        // 释放资源
        inputStreamReader.close();
        outputStreamWriter.close();
    }

    static void inputPractice() throws Exception {
        // 使用System.in实现
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while( true ){
            System.out.println("请输入字符串");
            String text = bufferedReader.readLine();
            System.out.println(text.toUpperCase());
            if ("e".equalsIgnoreCase(text) || "exit".equalsIgnoreCase(text)){
                System.out.println("程序结束");
                break;
            }
        }
        bufferedReader.close();
    }

    static void printStream(String srcPath) throws Exception {
        OutputStream outputStream = new FileOutputStream( new File(srcPath) );
        PrintStream printStream = new PrintStream(outputStream,true);
        if ( printStream != null) System.setOut(printStream);

        for (int i = 0; i < 255; i++) {
            System.out.print((char)i + "\t");
            if ((i + 1) % 10 == 0) System.out.println();
        }
        printStream.close();
    }

    static void printData() throws Exception {
        DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(new File("sample.txt")));

        dataOutputStream.writeUTF("阿伟输了！！！");
        dataOutputStream.flush();

        dataOutputStream.writeInt(1001);
        dataOutputStream.flush();

        dataOutputStream.writeBoolean(true);
        dataOutputStream.flush();

        dataOutputStream.close();
    }

    static void readData() throws Exception {
        DataInputStream dataInputStream = new DataInputStream(new FileInputStream(new File("sample.txt")));
        // 读取数据顺序，要按写入顺序保持一致
        String str = dataInputStream.readUTF();
        int i = dataInputStream.readInt();
        boolean b = dataInputStream.readBoolean();

        System.out.println(str);
        System.out.println(i);
        System.out.println(b);

        dataInputStream.close();
    }
}

